import jwt
from datetime import datetime, timedelta
# from django.conf import settings.dev

def generate_jwt_token(user):
    # 设置 JWT 的有效期，这里设置为 1 小时
    expiration_date = datetime.now() + timedelta(hours=1)

    # 准备要编码到 JWT 中的数据
    payload = {
        'user_id': user.get("id"),
        'username': user.get("username"),
        'exp': expiration_date.timestamp(),  # JWT 过期时间的时间戳
    }

    # 生成 JWT
    token = jwt.encode(payload, "123", algorithm='HS256')
    return token

user={"username":"admin","id":1}
print(jwt.decode(generate_jwt_token(user),"123", algorithms=['HS256']))